/*
* 文件：AutosetVersionNumber.cs
* 作者：王阳
* 时间：2019-5-20
* 描述：自动设置版本号
*/

using System;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;

namespace UnityXEditor
{
    [Serializable]
    public class AutosetVersionNumberSetting
    {
        [SerializeField]
        private bool m_Enabled = false;
        [SerializeField]
        private string m_Format = @"1.0.{年}{月}{日}";
        public bool enabled
        {
            get { return m_Enabled; }
            set
            {
                if (value != m_Enabled)
                {
                    m_Enabled = value;
                    Update();
                }
            }
        }
        public string format
        {
            get { return m_Format; }
            set
            {
                if (value != m_Format)
                {
                    m_Format = value;
                    Update();
                }
            }
        }
        public void OnGUI()
        {
            m_Enabled = EditorGUILayout.Toggle("自动设置版本号为当前日期", m_Enabled);
            EditorGUI.indentLevel++;
            {
                EditorGUI.BeginDisabledGroup(!m_Enabled);
                m_Format = EditorGUILayout.TextField(new GUIContent("版本号格式",
                    "占位符(长)：\n{年}：如2025\n{月}：如01\n{日}：如01\n{时}：如01\n{分}：如01\n{秒}：如01\n占位符(短)：\n[年]：如25\n[月]：如1\n[日]：如1\n[时]：如1\n[分]：如1\n[秒]：如1"), m_Format);
                EditorGUI.EndDisabledGroup();
            }
            EditorGUI.indentLevel--;
        }
        public void Update()
        {
        }
    }
    internal class AutosetVersionNumberBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport
    {
        public int callbackOrder => 0;
        public string originalVersion;
        //发布之前调用
        public void OnPreprocessBuild(BuildReport report)
        {
            if (!UnityXSettings.toolSettings.autosetVersionNumber.enabled) return;

            originalVersion = PlayerSettings.bundleVersion;

            DateTime dateTime = DateTime.Now;
            string newVersion = UnityXSettings.toolSettings.autosetVersionNumber.format;
            newVersion = newVersion.Replace("{年}", dateTime.Year.ToString());
            newVersion = newVersion.Replace("{月}", dateTime.Month.ToString().PadLeft(2, '0'));
            newVersion = newVersion.Replace("{日}", dateTime.Day.ToString().PadLeft(2, '0'));
            newVersion = newVersion.Replace("{时}", dateTime.Hour.ToString().PadLeft(2, '0'));
            newVersion = newVersion.Replace("{分}", dateTime.Minute.ToString().PadLeft(2, '0'));
            newVersion = newVersion.Replace("{秒}", dateTime.Second.ToString().PadLeft(2, '0'));

            newVersion = newVersion.Replace("[年]", (dateTime.Year % 100).ToString());
            newVersion = newVersion.Replace("[月]", dateTime.Month.ToString());
            newVersion = newVersion.Replace("[日]", dateTime.Day.ToString());
            newVersion = newVersion.Replace("[时]", dateTime.Hour.ToString());
            newVersion = newVersion.Replace("[分]", dateTime.Minute.ToString());
            newVersion = newVersion.Replace("[秒]", dateTime.Second.ToString());

            PlayerSettings.bundleVersion = newVersion;
        }
        //发布之后调用
        public void OnPostprocessBuild(BuildReport report)
        {
            if (!UnityXSettings.toolSettings.autosetVersionNumber.enabled) return;

            //PlayerSettings.bundleVersion = originalVersion;
            //AssetDatabase.SaveAssets();
        }
    }
}